<template>
  <div>
    <el-button type="primary" @click="getErrors">刷新</el-button>
    <el-button type="warning" @click="clear">清空错误</el-button>
    <div>
      显示已经解决的错误
      <el-checkbox v-model="showAll"/>
    </div>
    <el-table :data="list" border stripe>
      <el-table-column label="操作" width="100px">
        <template slot-scope="scope">
          <el-button type="text" size="small">已修复</el-button>
        </template>
      </el-table-column>
      <el-table-column label="数量" prop="count" width="100px"/>
      <el-table-column label="错误内容">
        <template slot-scope="scope">
          <pre>{{scope.row.message}}</pre>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script>
  export default {
    name: 'errorList',
    data() {
      return {
        showAll: false,
        list: [],
      };
    },
    methods: {
      async getErrors() {
        const res = await this.$http.get('/api/error?all=' + this.showAll);
        this.list = res.data;
      },
      async clear() {
        const confirm = await new Promise(resolve => {
          this.$confirm('确认清空？', {type: 'warning'}).then(() => {
            resolve(true);
          }).catch(() => {
            resolve(false);
          });
        });
        if (!confirm)
          return;
        await this.$http.get('/api/error/clear');
        this.$message({message: '操作成功', type: 'success'});
        this.getErrors();
      },
    },
    mounted() {
      this.getErrors();
    },
  };
</script>

<style scoped lang="scss">
  pre {
    word-break: break-all;
    white-space: pre-wrap;
    word-wrap: break-word;
  }
</style>